package com.firefightingiot.firefightingiot.config.scheduling;

import com.firefightingiot.firefightingiot.support.utils.DayUtil;
import com.firefightingiot.firefightingiot.support.utils.MyLogger;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.io.*;

//@Component
public class LogScheduling {

    @Resource
    public MyLogger logger;

    @Scheduled(cron="0 30 0 * * ?")//每晚1点删除今日表中的无用的记录：昨天的记录
    public void insertLogStatisticsForMonth() throws Exception {
        handleFile("/var/work/communication/cataline.log","/var/work/communication/");
        handleFile("/var/work/dataaccess/cataline.log","/var/work/dataaccess/");
        handleFile("/var/work/firefightingiot/cataline.log","/var/work/firefightingiot/");
        handleFile("/var/work/sequential/cataline.log","/var/work/sequential/");
        handleFile("/var/work/systemsetup/cataline.log","/var/work/systemsetup/");
        handleFile("/var/work/weifangfirecontrolformal/cataline.log","/var/work/weifangfirecontrolformal/");
    }

    //处理服务器的文件
    public static void handleFile(String nowFilePath, String basePath) throws Exception {
        //复制文件到一个昨天日期的文件
        copy(nowFilePath,basePath+ DayUtil.getYestoday() +".log");
        //清空现在的文件
        clear(nowFilePath);
        //删除大前天的文件
        System.out.println(basePath+ DayUtil.getBeforeNDay(3) +".log");
        deleteFile(basePath+ DayUtil.getBeforeNDay(3) +".log");
    }
    //复制文件
    public static void copy(String source, String dest) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(new File(source)));

            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(new File(dest)));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } finally {
            // 关闭流
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }
    //清空文件
    public static void clear(String source){
        File file = new File(source);
        BufferedWriter out = null ;
        try  {
            out = new BufferedWriter( new OutputStreamWriter(
                    new FileOutputStream(file,  false )));
            out.write("");
            out.flush();
        } catch  (Exception e) {
            e.printStackTrace();
        } finally  {
            try  {
                if (out != null)
                out.close();
            } catch  (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //删除文件
    private static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    public static void main(String[] args) throws Exception {
//        copy("d://aa.txt","d://"+ DayUtil.getYestoday() +".txt");
//        clear("d://aa.txt");
//        deleteFile("d://"+ DayUtil.getBeforeNDay(3) +".txt");
        handleFile("d://test/aa.log","d://test/");
    }

}
